home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TEST.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  5KB  |  220 lines

  1.  
  2. /*
  3.    Test program for checking basic CTask functions.
  4.  
  5.    Note that strange effects, like duplicated or incomplete output,
  6.    may be due to the concurrent usage of "printf". This is not trapped
  7.    here, since the effects are not fatal, but in real applications,
  8.    resources should be used to channel console output access.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. #include "tsk.h"
  17.  
  18. #if (TSK_NAMED)
  19. extern void snapshot (FILE *f);
  20. #endif
  21.  
  22. #define STACKSIZE 2048
  23.  
  24. word _stklen = 5 * STACKSIZE;  /* For Turbo C: Four tasks + main Task Stack */
  25.  
  26. typedef struct {
  27.                farptr xx;
  28.                char str [20];
  29.                } message;
  30.  
  31. tcb tcb1, tcb2, tcb3, tcb4;
  32. mailbox box;
  33. message msg;
  34. flag halt;
  35. pipe pip;
  36. buffer buf;
  37.  
  38. byte pipbuf [10];
  39. word bufbuf [40];
  40.  
  41. int endrun;
  42.  
  43.  
  44. /*
  45.    Task1 sends a mail to Task3, and waits for a response in the buffer.
  46.    The response is then displayed.
  47.    Task1 will stop while the halt flag is set.
  48. */
  49.  
  50. void far task1 (void)
  51. {
  52.    char str [20];
  53.  
  54.    printf ("Task 1 started\n");
  55.    while (!endrun)
  56.       {
  57.       wait_flag_clear (&halt, 0L);
  58.  
  59.       t_delay (5);
  60.       putch ('1');
  61.       strcpy (msg.str, "From T1");
  62.       send_mail (&box, &msg);
  63.  
  64.       read_buffer (&buf, str, 20, 0L);
  65.       printf ("Task 1 read buf: <%s>\n", str);
  66.       }
  67. }
  68.  
  69. /*
  70.    Task2 reads the keyboard. If a character has been read, it is passed
  71.    to Task4 via a pipe. Entering 'h' will set the halt flag (stopping Task1),
  72.    entering 'c' will clear the halt flag.
  73.    'e' stops the program.
  74. */
  75.  
  76. void far task2 (void)
  77. {
  78.    int ch;
  79.  
  80.    printf ("Task 2 started\n");
  81.    while (!endrun)
  82.       {
  83.       ch = t_read_key () & 0xff;
  84.       switch (tolower (ch))
  85.          {
  86.          case 'h':   set_flag (&halt);
  87.                      break;
  88.          case 'c':   clear_flag (&halt);
  89.                      break;
  90.          case 'e':   wake_task (NULL);
  91.                      break;
  92. #if (TSK_NAMED)
  93.          case 'd':   snapshot (stdout);
  94.                      break;
  95. #endif
  96.          }
  97.  
  98.       if (!endrun)
  99.          write_pipe (&pip, ch, 0L);
  100.       putch ('2');
  101.       }
  102. }
  103.  
  104.  
  105. /*
  106.    Task3 waits for mail, then sends it back through a buffer.
  107. */
  108.  
  109. void far task3 (void)
  110. {
  111.    message far *m;
  112.  
  113.    printf ("Task 3 started\n");
  114.    while (!endrun)
  115.       {
  116.       m = wait_mail (&box, 0L);
  117.       printf ("Task 3 received <%Fs>\n", m->str);
  118.  
  119.       m->str [6] = '3';
  120.       write_buffer (&buf, m->str, 7, 0L);
  121.       }
  122. }
  123.  
  124.  
  125. /*
  126.    Task4 waits for a character in the pipe and displays it. To make
  127.    things livelier, it uses a timeout while waiting, and will display
  128.    faces when the timeout occurred before the character.
  129. */
  130.  
  131. void far task4 (void)
  132. {
  133.    int ch;
  134.  
  135.    printf ("Task 4 started\n");
  136.    while (!endrun)
  137.       {
  138.       ch = read_pipe (&pip, 10L);
  139.       if (ch < 0)
  140.          putch (0x02);
  141.       else
  142.          printf ("Task 4 got <%c>\n", ch);
  143.       }
  144. }
  145.  
  146.  
  147. main ()
  148. {
  149.    char stack1 [STACKSIZE];
  150.    char stack2 [STACKSIZE];
  151.    char stack3 [STACKSIZE];
  152.    char stack4 [STACKSIZE];
  153.  
  154.    endrun = 0;
  155.    install_tasker (0, 0);
  156.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL
  157. #if (TSK_NAMEPAR)
  158.                 ,"TASK1"
  159. #endif
  160.                 );
  161.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL
  162. #if (TSK_NAMEPAR)
  163.                 ,"TASK2"
  164. #endif
  165.                 );
  166.    create_task (&tcb3, task3, stack3, STACKSIZE, PRI_STD, NULL
  167. #if (TSK_NAMEPAR)
  168.                 ,"TASK3"
  169. #endif
  170.                 );
  171.    create_task (&tcb4, task4, stack4, STACKSIZE, PRI_STD, NULL
  172. #if (TSK_NAMEPAR)
  173.                 ,"TASK4"
  174. #endif
  175.                 );
  176.    create_mailbox (&box
  177. #if (TSK_NAMEPAR)
  178.                 ,"Mailbox"
  179. #endif
  180.                 );
  181.    create_flag (&halt
  182. #if (TSK_NAMEPAR)
  183.                 ,"Halt"
  184. #endif
  185.                 );
  186.    create_pipe (&pip, pipbuf, sizeof (pipbuf)
  187. #if (TSK_NAMEPAR)
  188.                 ,"Pipe"
  189. #endif
  190.                 );
  191.    create_buffer (&buf, bufbuf, sizeof (bufbuf)
  192. #if (TSK_NAMEPAR)
  193.                 ,"Buffer"
  194. #endif
  195.                 );
  196.  
  197.    start_task (&tcb1);
  198.    start_task (&tcb2);
  199.    start_task (&tcb3);
  200.    start_task (&tcb4);
  201.    preempt_on ();
  202.  
  203.    t_delay (0L);
  204.  
  205.    endrun = 1;
  206.    puts ("******** Main Task *********");
  207.  
  208.    set_priority (NULL, 10);
  209.    delete_mailbox (&box);
  210.    delete_pipe (&pip);
  211.    delete_buffer (&buf);
  212.    delete_flag (&halt);
  213.  
  214.    schedule ();
  215.  
  216.    preempt_off ();
  217. /*   remove_tasker (); */
  218.    puts ("******** End Run *********");
  219. }
  220.